package com.jade.zsy.base.common;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: JinYa 2009_jinya@163.com
 * Date: 2016-4-20
 * Time: 19:03
 * Version : 1.0
 * Remark:
 * History:
 */
public class ConstantUtil {
    public final static String FILE_STORAGE_PATH_UPLOAD = "/jade/yem/storage/temp/";
    public final static  String QINIU_VISTOR_URL="http://oaq77popm.bkt.clouddn.com/";

    public final static String FILE_STORE_WX_PER_UPLOAD = "/jade/yem/storage/pic/per/";//永久目录
    public final static String FILE_STORE_WX_TEMP_UPLOAD = "/jade/yem/storage/pic/temp/";//临时目录


    public final static  String QINIU_AK="OTEpg76faknaSWkEjN6zCRFgNYYwG_FgK3gNG1Gl";
    public final static  String QINIU_SK="m16qDTG15Dh29u2XYUaoP3bFB5T811eMadPiGTpz";
    public final static  String QINIU_BUCKETNAME="bishop";

    public final static String DEFAULT_PIC_STORE = "http://oaq77popm.bkt.clouddn.com/default/store.png";//默认店铺图片
    public final static String DEFAULT_PIC_CRM = "http://oaq77popm.bkt.clouddn.com/default/crm.png";//默认会员头像
    public final static String DEFAULT_PIC_STAFF = "http://oaq77popm.bkt.clouddn.com/default/staff.png";//默认员工头像
    public final static String DEFAULT_PIC_GOODS = "http://oaq77popm.bkt.clouddn.com/default/good.png";//默认商品头像

    /** 没有memcache的环境，为false;有memcache的环境，为true */
    public final static boolean HAVE_MEMCACHE =true;
    /** 没有memcache的环境，为false;有memcache的环境，为true */
    public final static String RETURN_JSON_KEY = "rows";
    //默认分页页码
    public final static int CURRENT_PAGE=1;
    //默认分页页码（设置默认参数需要字符串型）
    public final static String DEFAULT_CURRENT_PAGE = "1";
    //默认的分页记录条数
    public final static int PAGING_SIZE = 10;
    //默认的分页记录条数（设置默认参数需要字符串型）
    public final static String DEFAULT_PAGING_SIZE = "10";

    public final static String charset = "UTF-8";
    public final static String signType = "MD5";
    public final static String CURRENT_LOGIN_USER_STORE = "currentLoginUserStore";
    //当前登陆账号的对象
    public final static String CURRENT_USER_COMPANY_WX_CONFIG = "LoginUserWxConfigObject";//当前微信配置
    public final static String CURRENT_USER_COMPANY_ALI_CONFIG = "LoginUserWxAliConfigObject"; //当前支付宝配置信息
    //当前登陆账号的对象
    public final static String CURRENT_USER_ACCOUNT_OBJECT = "LoginUserAccountObject";

    public final static String INIT_DATE_FORMATE_DATE = "yyyy-MM-dd";
    public final static String INIT_DATE_FORMATE_TIME = "yyyy-MM-dd HH:mm";
    public final static String SORT_ASC = "asc";
    public final static String SORT_DESC = "desc";
    public final static String SORT_ITEM = "createTime";
    public final static ArrayList<String> sortRule = new ArrayList<String>(){{add("asc"); add("desc");}};
    public final static ArrayList<String> sortStore = new ArrayList<String>(){{add("name"); add("category");}};

    public final static int RECORD_IS_ACTIVE = 0;
    public final static int RECORD_NO_ACTIVE = 1;

    public final static int CUSTOMER_SUPER_ADMIN = 10;//超级管理员
    public final static int CUSTOMER_ADMIN = 7;//一般管理员
    public final static int CUSTOMER_NORMAL = 1;//普通用户

    public final static int CUSTOMER_STOP = 9; //用户已停用
    /**
     * 获得当前请求的IP地址
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        //ipAddress = this.getRequest().getRemoteAddr();
        ipAddress = request.getHeader("x-forwarded-for");
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if(ipAddress.equals("127.0.0.1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress= inet.getHostAddress();
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
            if(ipAddress.indexOf(",")>0){
                ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }
}
